<template>
  <div id="app">
    <h2>测试题</h2>
    <button @click="generateProblems">生成题目</button>
    <SubjectItem v-for="(item, index) in problems" :key="index" :item="item" @result="item.answer = $event">
    </SubjectItem>
    <div>
      <FlagItem v-for="(item, index) in problems" :key="index" :answer="item.answer">{{ index + 1
        }}.{{ answer(item.answer) }}</FlagItem>
    </div>
  </div>
</template>

<script>
import SubjectItem from './components/SubjectItem.vue';
import FlagItem from './components/FlagItem.vue';
export default {
  components: {
    SubjectItem,
    FlagItem,
  },
  data() {
    return {
      problems: []
    }
  },
  methods: {
    generateProblems() {
      //清空之前的题目
      this.problems = [];
      for (let i = 0; i < 5; i++) {
        // eslint-disable-next-line no-unused-vars
        const num1 = Math.floor(Math.random() * 100);
        // eslint-disable-next-line no-unused-vars
        const num2 = Math.floor(Math.random() * 100);
        const answer = null;
        this.problems.push({ num1, num2, answer });
      }
    },
    answer(value) {
      if (value === null) {
        return '未完成'
      } else if (value) {
        return '回答正确'
      } else {
        return '回答错误'
      }
    }
  },
  created() {
    this.generateProblems();
  },
};
</script>

<style>
body {
  background-color: #eee;
}

#app {
  background-color: #fff;
  width: 500px;
  margin: 50px auto;
  box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.5);
  padding: 2em;
}
</style>